# delimit ;
clear ;
cd "replication" ;
set more off ; 

* *************************************************************************** ;
* re-run endline analysis
* *************************************************************************** ;

cd ".." ;
do "./replication/do/table05-endline-behavior.do" ; 

* **************************************************************************** ;
* merge with baseline to get full sample list including attriters
* **************************************************************************** ;

merge 1:1 Id using "./data/baseline", keepusing(Id wave class neighborhood female mfi healthonly healthandpay control) ;
tab _merge ;
assert _merge != 1 ; 

* selection indicator to be used in Lee bounds ;
gen selection_indicator = _merge == 3 ; 

* logical check: full sample is 887 respondents ;
qui des ;
assert r(N) == 887 ;

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

* need to do this again so that the attriters also have values for strata/waveclass ;
drop strata waveclass ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

* *****************************************************************************
* robustness: extreme value (manski) bounds 
* ***************************************************************************** ;

est drop _all ;

foreach var in behav_all { ;

	* lower ; 
	gen l`var' = `var' ;
	replace l`var' = 0 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace l`var' = 6 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	label var l`var' "Lower" ;
	
	areg l`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskil ;
	
	* upper ; 
	gen u`var' = `var' ;
	replace u`var' = 6 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace u`var' = 0 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	
	areg u`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskiu ; 
	label var u`var' "Upper" ;
	
} ;

foreach var in $module1_behav $module23_behav $module4_behav $module5_behav  { ;
	
	* lower ; 
	gen l`var' = `var' ;
	replace l`var' = 0 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace l`var' = 1 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	label var l`var' "Lower" ;
	
	areg l`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskil ;
	
	* upper ; 
	gen u`var' = `var' ;
	replace u`var' = 1 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace u`var' = 0 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	
	areg u`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskiu ; 
	label var u`var' "Upper" ;
	
} ;

* ***************************************************************************** ;
* output, panel A: results from manski bounds
* ***************************************************************************** ;

esttab *manski* using "./output/appendix-table-endline-behavior-bounds-panelA.tex",
	replace
	drop(_cons)
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	stats(N, fmt(%9.0f) labels("N"))	substitute(\_ _)
	prehead(\begin{table}[p] \centering `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Bounds for Effects on Health Behaviors}
		\label{appendix-table-endline-behavior-bounds}
	\setlength{\tabcolsep}{7.25pt}\scriptsize
	\begin{tabular}{p{0.2cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1cm}}
		\toprule)
	legend label booktabs collabels( , none)
	mgroups(
	"\footnotesize{\shortstack[l]{Total\\no. of\\behaviors\\adopted}}"
	"\footnotesize{\shortstack[l]{Drinks\\filtered\\or\\boiled\\water}}"
	"\footnotesize{\shortstack[l]{Uses\\a water\\vessel\\with tap}}"
	"\footnotesize{\shortstack[l]{Washes\\hands\\with\\soap\\at least\\thrice\\daily}}"
	"\footnotesize{\shortstack[l]{Would\\go for\\ante-natal\\care if\\self/spouse\\pregnant}}" 
	"\footnotesize{\shortstack[l]{Would\\go for\\hospital\\delivery if\\self/spouse\\pregnant}}" 
	"\footnotesize{\shortstack[l]{Has been\\tested\\for HIV\\in the\\last 3\\months}}"
	"\footnotesize{\shortstack[l]{Uses\\condom\\during\\inter-\\course}}"
	"\footnotesize{\shortstack[l]{Eats more\\nutritious\\foods to\\prevent\\night-\\blindness}}"
	, pattern(1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	postfoot(`"\midrule"'  \end{tabularx}) 
	posthead(\midrule \end{tabular}
	\bigskip
	\caption*{Panel A: Manski Bounds} 
	\vspace{-0.5ex}
	\newcolumntype{C}{>{\centering\arraybackslash}X} \setlength{\tabcolsep}{0.25pt}\scriptsize
	\begin{tabularx}{\linewidth}{lCCCCCCCCCCCCCCCCCC}
	\midrule
	&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)}
	&\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)}
	&\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)} &\multicolumn{1}{c}{(9)}
	&\multicolumn{1}{c}{(10)}&\multicolumn{1}{c}{(11)}&\multicolumn{1}{c}{(12)}
	&\multicolumn{1}{c}{(13)}&\multicolumn{1}{c}{(14)}&\multicolumn{1}{c}{(15)}
	&\multicolumn{1}{c}{(16)}&\multicolumn{1}{c}{(17)}&\multicolumn{1}{c}{(18)}
	\\ \midrule) ;
			
* *****************************************************************************
* robustness: lee bounds
* ***************************************************************************** ;

* save results within the data, which I will then output using texsave ;
gen variable = "" ; 
replace variable = "HEE" in 1 ;
replace variable = "HEEC" in 3 ;
label var variable "" ;

foreach var in behav_all $module1_behav $module23_behav $module4_behav $module5_behav  { ;
	gen `var'_l = "" ;
	gen `var'_u = "" ;
	
	label var `var'_l "" ;
	label var `var'_u "" ;
} ;

foreach var in behav_all $module1_behav $module23_behav $module4_behav $module5_behav  { ;

	di "************************* outcome: `var' *************************" ;

	* for the variable healthonly (HEE), calculate lee bounds ;
	* HEE arm vs. control arm, so HEEC arm is excluded from the command ; 
	
	leebounds `var' healthonly if healthonly == 1 | control == 1 , select(selection_indicator) ; 
	
	* get coeffs ;
	replace `var'_l  = string(e(b)[1,1], "%9.3f") in 1 ;
	replace `var'_u  = string(e(b)[1,2], "%9.3f") in 1 ; 
	
	* get SEs ;
	replace `var'_l = "(" + string(sqrt(e(V)[1,1]), "%9.3f") + ")" in 2 ; 
	replace `var'_u = "(" + string(sqrt(e(V)[2,2]), "%9.3f") + ")" in 2 ; 
	
	* add stars  ; 
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.64 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "*" in 1 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.96 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "**" in 1 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 2.58 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "***" in 1 ;

	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.64 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "*" in 1 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.96 replace `var'_u  =  string(e(b)[1,2], "%9.3f") + "**" in 1 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 2.58 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "***" in 1 ;

	* for the variable healtandpay (HEEC), calculate lee bounds ;
	* HEEC arm vs. control arm, so HEE arm is excluded from the command ; 
	
	leebounds `var' healthandpay if healthandpay == 1 | control == 1, select(selection_indicator) ; 
	
	* get coeffs ;
	replace `var'_l  = string(e(b)[1,1], "%9.3f") in 3 ;
	replace `var'_u  = string(e(b)[1,2], "%9.3f") in 3 ; 
	
	* get SEs; 
	replace `var'_l = "(" + string(sqrt(e(V)[1,1]), "%9.3f") + ")"  in 4 ; 
	replace `var'_u = "(" + string(sqrt(e(V)[2,2]), "%9.3f") + ")" in 4 ; 	

	* add stars  ; 
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.64 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "*" in 3 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.96 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "**" in 3 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 2.58 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "***" in 3 ;

	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.64 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "*" in 3 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.96 replace `var'_u  =  string(e(b)[1,2], "%9.3f") + "**" in 3 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 2.58 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "***" in 3 ;

} ;

* ***************************************************************************** ;
* output, panel B: results from lee bounds
* ***************************************************************************** ;

keep variable behav_all_* water_boil_filter_* water_tap_* wash_soap3_* pregnancy_anc_* pregnancy_hospital_* aids_tested_self_* condom_use_* nightblindness_food_* ;
dropmiss, obs force ;
texsave using "./output/appendix-table-endline-behavior-bounds-panelB.tex", frag replace title("Panel B: Lee Bounds") autonumber varlabels ;

filefilter "./output/appendix-table-endline-behavior-bounds-panelB.tex" "./output/appendix-table-endline-behavior-bounds-panelB-v00.tex", from("\BStoprule") to("\BSmidrule") replace ;
filefilter "./output/appendix-table-endline-behavior-bounds-panelB-v00.tex" "./output/appendix-table-endline-behavior-bounds-panelB-v01.tex", from("\BSbegin{table}[tbp] \BScentering") to("\BSbigskip") replace ;
filefilter "./output/appendix-table-endline-behavior-bounds-panelB-v01.tex" "./output/appendix-table-endline-behavior-bounds-panelB-v02.tex", from("{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{}&{} \BStabularnewline") to("") replace ;
filefilter "./output/appendix-table-endline-behavior-bounds-panelB-v02.tex" "./output/appendix-table-endline-behavior-bounds-panelB-v03.tex", from("\BSmidrule \BSaddlinespace[\BSbelowrulesep]") to("") replace ;
filefilter "./output/appendix-table-endline-behavior-bounds-panelB-v03.tex" "./output/appendix-table-endline-behavior-bounds-panelB-v04.tex", 
from("\BScaption{Panel B: Lee Bounds}") to("\BScaption*{Panel B: Lee Bounds}") replace ;
filefilter "./output/appendix-table-endline-behavior-bounds-panelB-v04.tex" "./output/appendix-table-endline-behavior-bounds-panelB.tex", 
from("\BSend{table}") to("\BScaptionsetup{justification=justified, width=\BSlinewidth} \BScaption*{\BSfootnotesize \BStextit{Notes:} This table presents bounds on the treatment effects estimated in Table \BSref{table-endline-behavior}. Panel A uses extreme value (Manski) bounds following \BStextcite{horowitz2000nonparametric}. Panel B uses trimming bounds following \BStextcite{lee2009training} and is estimated with no covariates.} \BSend{table}") replace ;

forvalues i = 0/4 { ;
	erase "./output/appendix-table-endline-behavior-bounds-panelB-v0`i'.tex" ;
} ;

exit ;

